﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using _4T.Data;
using CMS.Core.Libs;
using _4T.Data.IDataContext;

namespace _4T.DAL
{
    public class CommentDAL
    {
        public List<Comment> GetListComment(string ProfileCode, int Stage)
        {
            var conn = new SqlConnection(Ultility.connectionString);
            conn.Open();
            var cmd = new SqlCommand();
            cmd.CommandText = "GetListComment";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@ProfileCode", SqlDbType.NVarChar, 50)).Value = ProfileCode;
            cmd.Parameters.Add(new SqlParameter("@Stage", SqlDbType.Int, Int32.MaxValue)).Value = Stage;
            cmd.Connection = conn;
            var reader = cmd.ExecuteReader();

            if (reader != null && reader.HasRows)
            {
                return reader.DatareaderToList(n => new Comment
                {
                    CommentID = (Int64)n["CommentID"],
                    CreatedDate = (DateTime)n["CreatedDate"],
                    Body = (String)n["Body"],
                    CommentBy = (String)n["FullName"],
                    PersonalCode = (String)n["PersonalCode"],
                    ProfileCode = (String)n["ProfileCode"],
                    Stage = (int)n["Stage"]
                }).ToList();
            }
            else
            {
                return new List<Comment>();
            }
        }

        public int InsertComment(string PersonalCode, string ProfileCode, int Stage, string body)
        {
            var conn = new SqlConnection(Ultility.connectionString);
            conn.Open();
            var cmd = new SqlCommand();
            cmd.CommandText = "InsertComment";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Body", SqlDbType.NVarChar, 250)).Value = body;
            cmd.Parameters.Add(new SqlParameter("@CommentBy", SqlDbType.NVarChar, 50)).Value = PersonalCode;
            cmd.Parameters.Add(new SqlParameter("@ProfileCode", SqlDbType.NVarChar, 50)).Value = ProfileCode;
            cmd.Parameters.Add(new SqlParameter("@Stage", SqlDbType.Int, Int32.MaxValue)).Value = Stage;
            cmd.Connection = conn;
            object cmdResult = cmd.ExecuteScalar();
            int result = cmdResult is DBNull ? 0 : Convert.ToInt32(cmdResult);

            return result;
        }
    }
}